package com.yxhl.zoume.core.officialcar.presenter;

import android.content.Context;

import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.MpvModelRequest;
import com.yxhl.protobuf.MpvModelResponse;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.officialcar.view.SingleWayQueryView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.car.officialcar.QueryPriceAndDistanceUseCase;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/8/25.
 */
public class SingleWayQueryPresenter extends BasePresenter {


    @Inject
    QueryPriceAndDistanceUseCase mQueryPriceAndDistanceUseCase;
    private SingleWayQueryView mSingleWayQueryView;
    /**
     * 出发点 经度
     */
    private String lng;
    private String lat;
    /**
     * 到达地 经度
     */
    private String endLng;
    private String endLat;

    private String vehicleType;


    @Inject
    public SingleWayQueryPresenter(Context context) {
        super(context);
    }


    public void queryPriceAndDistance(OrderBizType orderBizType) {

        MpvModelRequest.Builder mpvModelRequestBuilder = MpvModelRequest.newBuilder()
                .setCommonRequest(getCommonReqParams())
                .setBindType(orderBizType)
                .setLng(lng)
                .setLat(lat)
                .setElat(endLat)
                .setElng(endLng)
                .setVehicleType(vehicleType);

        mQueryPriceAndDistanceUseCase
                .execute(mpvModelRequestBuilder.build(),new ZMSubscriber<MpvModelResponse>() {
                    @Override
                    public void onNext(MpvModelResponse mpvModelResponse) {
                        super.onNext(mpvModelResponse);
                        CommonRespParams commonResponse = mpvModelResponse.getCommonResponse();
                        if (commonResponse != null) {
                            String isSucc = commonResponse.getIsSucc();
                            if ("true".equalsIgnoreCase(isSucc)) {
                                String desc = mpvModelResponse.getDesc();
                                mSingleWayQueryView.renderQueryPriceAndDistanceOnSuccess(desc);
                            } else {
                                String resultMsg = commonResponse.getResultMsg();
                                mSingleWayQueryView.renderQueryPriceAndDistanceOnFailure(resultMsg);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSingleWayQueryView.renderQueryPriceAndDistanceOnError(e.toString());
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });

    }


    @Override
    public void attachView(BaseView view) {
        if (view instanceof SingleWayQueryView) {
            mSingleWayQueryView = (SingleWayQueryView) view;
        }
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public void setEndLng(String endLng) {
        this.endLng = endLng;
    }

    public void setEndLat(String endLat) {
        this.endLat = endLat;
    }

    public void setVehicleType(String vehicleType) {
        this.vehicleType = vehicleType;
    }
}
